package com.logos.servlet.req;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebServlet(urlPatterns = "/test",loadOnStartup = 0)
public class ServletTest implements Servlet {

    private ServletConfig servletConfig;


    /**
     * 服务方法 每一次Servlet被访问时调用
     */
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("hello servlet");
        // 根据请求方式不同，进行分别的处理
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        // 1.获取请求方式
        String method = request.getMethod();
        // 2.判断
        if("GET".equals(method)) {
            // get方式的处理逻辑
            System.out.println("get...");
        }else if("POST".equals(method)) {
            // post方式的处理逻辑
            System.out.println("post...");
        }
    }

    /**
     * 初始化方法 Servlet第一次被访问时调用
     * loadOnStartup>=0 项目启动时就初始化
     */
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        this.servletConfig = servletConfig;
        System.out.println("init...");
    }

    /**
     * 销毁方法 内存释放或者服务器关闭时调用
     */
    @Override
    public void destroy() {
        System.out.println("destroy...");
    }

    @Override
    public ServletConfig getServletConfig() {
        return servletConfig;
    }

    @Override
    public String getServletInfo() {
        return null;
    }

}
